package com.lkl.hystrixdemo.order.zookeeper;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZkUtils {
    private static String conString = "127.0.0.1:2181";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static String path = "/liaokailei";

    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper(conString, 60 * 1000, watchedEvent ->  {
                System.out.println("事件回调通知到了...");

                //监听ZK连接的状态
                Watcher.Event.KeeperState type = watchedEvent.getState();

                if (Watcher.Event.KeeperState.SyncConnected.name().equals(type.name())) {
                    System.out.println("zookeeper， 连接成功" );
                }
                countDownLatch.countDown();
        });
        countDownLatch.await();
        String node = zooKeeper.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("节点创建成功：" + node);

        Thread.sleep(5 * 1000);
        zooKeeper.close();
    }

}
